# _*_coding:utf-8_*_
import threading
import socket
from ATS.socketServer import SocketServer_constant


class SocketClient(threading.Thread):
    def __init__(self, server_ip=None):
        threading.Thread.__init__(self)
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if server_ip is None:
            server_ip = socket.gethostbyname(socket.gethostname())

        self.server_ip = server_ip
        self.port = SocketServer_constant.LIST_PORT

    def send_msg(self, msg):
        return self.socket.send(msg)

    def run(self):
        print('client started and connect to server!\n')
        try:
            self.socket.connect((self.server_ip, self.port))
        except Exception as arg:
            print("connect server fail:")
            self.socket.close()
            return
        while 1:
            try:
                #                self.socket.send('run-ATS_SingleTestPlan.ppf#')
                self.socket.send('update-Continue_Flag=True#'.encode("utf"))
            except ConnectionAbortedError as msg:

                break
            #            time.sleep(3)
            print(self.socket.recv(64))
            break  # run automation once only


# client = SocketClient()
# client.start()
# client.join()
# client.socket.close()
